<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Gift;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class News extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();
        self::checktoken($data['id']);
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $rows = $data['listrow'] : $rows = 20;
        $res = \db('home_news')
            ->alias('N')
            ->join('home_users U','N.fromid=U.id')
            ->join('home_users S','N.toid=S.id')
            ->where('N.toid',$data['id'])
            ->field('N.id,N.fromid,U.nickname fromname,N.toid,
            U.nickname toname,U.headicon,N.sqid,N.create_time,N.type,N.news_content,N.status')
            ->order('N.create_time desc,N.status asc')
            ->paginate($rows,true,['page'=>$page])->each(function ($item){
                \db('home_news')->where('id',$item['id'])->update(['status'=>2]);
                \db('home_apply')->where('toid',$item['id'])->update(['status'=>2]);
                return $item;
            });
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    

}
